//
//  XFDraggableButton.m
//  FreeTime
//
//  Created by 王江华 on 16/9/19.
//  Copyright © 2016年 SRDZ.com. All rights reserved.


#import "XFDraggableButton.h"

@implementation XFDraggableButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    //    NSLog(@"touchesEnded");
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    // Retrieve the touch point 检索接触点
    CGPoint pt = [[touches anyObject] locationInView:self];
    startLocation = pt;
    //    [[self superview] bringSubviewToFront:self];
    //    NSLog(@"touchesBegan location x is %.0f, y is %.0f",pt.x,pt.y);
    //    NSLog(@"touches.count is %d",touches.count);
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    CGRect frame = [self frame];
    CGPoint pt = [[touches anyObject] locationInView:self];
    
    if (pt.x - startLocation.x + frame.origin.x >= _limitLeft && pt.x - startLocation.x + frame.origin.x <= _limitRight) {
        frame.origin.x += pt.x - startLocation.x;
        //        frame.origin.y += pt.y - startLocation.y;
    }else if (pt.x - startLocation.x + frame.origin.x < _limitLeft){
        frame.origin.x = _limitLeft;
    }else{
        frame.origin.x = _limitRight;
    }
    
    [self setFrame:frame];
    
    if (_delegate && [_delegate respondsToSelector:@selector(dragWithButton:)]) {
        [_delegate dragWithButton:self];
    }
    //    NSLog(@"touchesMoved pt.x is %.0f, pt.y is %.0f",pt.x,pt.y);
    //    NSLog(@"touchesMoved startLocation.x is %.0f, startLocation.y is %.0f",startLocation.x,startLocation.y);
    //    NSLog(@"touchesMoved x is %.0f, y is %.0f",frame.origin.x,frame.origin.y);
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */
@end
